Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

108
Vistas
Displaying favico image from database to html using javascript shows "undefined"

I have read a lot of similar examples here on S.O yet, I cannot figure out what is the problem. The item.picture image is successfully displayed on the html and its url can also be printed on the console.

However, the favico displays undefined both on the console and on html.
Here is the sample code:

$(function () {
  jQuery.ajax({
    type:'GET',
    url:"api/wedding/?format=json&limit=20",
    dataType:"json",
    success :function (data) {
      var listA=$("#wedding_id");
      for(var i=0;i<data.results.length;i++) {
        var  item=newDiv(data.results[i])
        listA.append(item);
      }
    },
    error:function (e) {
      console.log(e) // Better for debugging
      // alert("Error" + e)
    }
  });
});

function newDiv(item)
  {
  if (item.pic) {
    var wed_pic = item.pic.split(",")[0];
  }
  if (item.favico) {
    var favicon = item.favico.split("/")[0]; 
  }
  console.log(item.pic); // <--- can display the pic_url on console
  console.log(favicon); //shows  'undefined'
 
  var template = '<ul class="wedding-list" id='+ item.wedding_id +'>'
               + '<a class="wedding-icon" href="#">' + favicon + '</a>'  //<--- where I think lies the problem
               + '<a class="wed_img" target="_blank" href=' + item.url + '>' 
               + '<img src=' + wed_pic + '>' 
               + '</a> </ul>' +
  return template
  }

What could possibly be the cause of the undefined error? Thank you for helping.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

for a start

const listA = document.querySelector('#wedding_id')
  ;
fetch ('api/wedding/?format=json&limit')
  .then (res => res.json())
  .then (data =>
    {
    for (let row of data.results)
      listA.appendChild( newDiv(row))  
    })
  .catch (err => console.error(err)) 
  ; 
function newDiv(item)
  {
  let
    e_UL    = document.createElement('ul')
  , wed_pic = !!item?.pic    ? item.pic.split(',')[0]    : ''
  , favicon = !!item?.favico ? item.favico.split('/')[0] : ''
    ;
  console.log('item ----->', JSON.stringify(item,0,2) )
  console.log('wed_pic  ->', wed_pic  )
  console.log('favicon  ->', favicon  ) 
  
  e_UL.id        = item.wedding_id
  e_UL.className = 'wedding-list'
  e_UL.innerHTML = ` 
    <a class="wedding-icon" href="#"> ${favicon} </a> 
    <a class="wed_img" target="_blank" href="${item.url}">
      <img src="${wed_pic}">
    </a>`
    
  return e_UL
  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda